#ifndef MATCHER_H
#define MATCHER_H

#include <cassert>
#include <string>
#include <map>
#include <stdexcept>

struct Span{
    Span(int _from=-1,int _to=-1){
        from = _from;
        to = _to;
    }

    void set(int f,int t){
        from = f;
        to = t;
    }

    int from,to;
};

class Pattern;

class GroupNotExist : public std::logic_error{
public:
    GroupNotExist(const std::string& msg)
        :logic_error(msg){}
};

class Matcher
{
public:
    Matcher(Pattern* _p,const std::string& _text)
        :text(_text)
    {
        p = _p;        
        last_match_to = -1;
        text = _text;
    }


    void setFrom(int _from){from = _from;}

    int  getFrom(){ return from;}
    int  getTo() { return last_match_to;}

    void setLastMatchTo(int index){ last_match_to = index;}
    int  getLastMatchTo() { return last_match_to;}


    std::string group(int index);
    std::string group(const char* name);
    Span        groupSpan(int index);
    Span        groupSpan(const char *name);
private:
    Pattern* p;
    std::string text;
    int last_match_to;
    int from;

    friend class Group;
    friend class GroupTail;
    Span         capture_groups[100];

    std::string getSubstr(const Span& s){
        if(s.from==-1)
            return "";
        return text.substr(s.from,s.to-s.from+1);
    }
};


#endif // MATCHER_H
